/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.ProcessDefine;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.ProcessDefineService;
import me.zhengjie.modules.system.domain.vo.ProcessDefineQueryCriteria;
import me.zhengjie.modules.system.mapper.ProcessDefineMapper;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author pysduck
* @date 2024-09-03
**/
@Service
@RequiredArgsConstructor
public class ProcessDefineServiceImpl extends ServiceImpl<ProcessDefineMapper, ProcessDefine> implements ProcessDefineService {

    private final ProcessDefineMapper processDefineMapper;
    private final RuntimeService runtimeService;
    private final RepositoryService repositoryService;

    @Override
    public PageResult<ProcessDefine> queryAll(ProcessDefineQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(processDefineMapper.findAll(criteria, page));
    }

    @Override
    public List<ProcessDefine> queryAll(ProcessDefineQueryCriteria criteria){
        return processDefineMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProcessDefine resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProcessDefine resources) {
        ProcessDefine processDefine = getById(resources.getId());
        processDefine.copy(resources);
        saveOrUpdate(processDefine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void startProcess(Integer id) {
        ProcessDefine processDefine = getById(id);
        if (processDefine.getIsStart() == 1){
            throw new RuntimeException("流程已启动");
        }
        try {
//            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                    .deploymentId(processDefine.getFlowId())
//                    .singleResult();
            runtimeService.startProcessInstanceById(processDefine.getFlowId());
            processDefine.setIsStart(1);
            processDefineMapper.updateById(processDefine);
        }catch (Exception e){
            throw new RuntimeException("启动流程失败");
        }
    }

    @Override
    public void download(List<ProcessDefine> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProcessDefine processDefine : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("流程名", processInstance.getFlowName());
//            map.put("流程设计json对象", processInstance.getOrginaldata());
//            map.put("定义版本", processInstance.getVersion());
//            map.put("流程设计ID", processInstance.getProcessDesignId());
//            map.put("流程ID", processInstance.getFlowId());
//            map.put("状态", processInstance.getStatus());
//            map.put("创建人", processInstance.getCreateBy());
//            map.put("修改人", processInstance.getUpdateBy());
//            map.put("创建时间", processInstance.getCreateTime());
//            map.put("修改时间", processInstance.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}